package com.qnzw.zjmy.utils;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.qnzw.zjmy.R;

import cn.hutool.core.util.StrUtil;

public class ViewUtils {

    public static void removeFromParent(View view) {
        if (view != null) {
            ViewParent vp = view.getParent();
            if (vp instanceof ViewGroup) {
                ((ViewGroup) vp).removeView(view);
            }
        }
    }


    /**
     * 风控提示框
     *
     * @param context
     * @param content
     * @param copyContent
     */
    public static void showRisk(Context context, String content, String copyContent) {
        //判断提现账号
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        View root = LayoutInflater.from(context).inflate(R.layout.pop_risk, null);
        dialog.setView(root);
        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) TextView tvContent = root.findViewById(R.id.tv_content);
        ImageView imageView = root.findViewById(R.id.iv_qr_code);
        if(StrUtil.isEmpty(copyContent)){
            imageView.setVisibility(View.GONE);
        }
        Bitmap qrCode = QrcodeUtils.createQRImage(copyContent, 500, 500);
        imageView.setImageBitmap(qrCode);
        tvContent.setText(content);
        AlertDialog alertDialog = dialog.create();
        alertDialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
        alertDialog.setCancelable(false);
        alertDialog.show();
    }

    /**
     * 系统剪贴板-复制:   为内容
     *
     * @param context
     * @param s
     */
    public static void copy(Context context, String s) {
        // 获取系统剪贴板
        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建一个剪贴数据集，包含一个普通文本数据条目（需要复制的数据）
        ClipData clipData = ClipData.newPlainText(null, s);
        // 把数据集设置（复制）到剪贴板
        clipboard.setPrimaryClip(clipData);
        Toast.makeText(context, "复制成功", Toast.LENGTH_SHORT).show();
    }

}
